﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.IO;
using System.Linq;
using System.Linq.Dynamic;
using System.Reflection;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Services;
using System.Web.Services.Protocols;
using Dimok;
using Dimok.Exceptions;
using Data.LINQ;
using Dimok.Web.UI;
namespace WSA {
	[WebService(Namespace = "http://tempuri.org/")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	[System.Web.Script.Services.ScriptService]
	public class Bridge : Common {

		#region Note
		[WebMethod]
		public void Note_Delete(Dictionary<string, int> Keys) {
			try {
				var mb = new MotoBank();
				var row = mb.t_Notes.Where(s => s.ID == Keys["ID"]).Single();
				mb.t_Notes.DeleteOnSubmit(row);
				mb.SubmitChanges();
			} catch (Exception exc) { ASPException.Publish(exc); throw exc; }
		}
		[WebMethod]
		public t_Note Note_Add(t_Note Note) {
			try {
				new MotoBank().t_Notes.Insert(new Dictionary<string, object>() { 
				{ "LoanID", Note.LoanID+"" } ,{"NoteTypeID",Note.NoteTypeID+""},{"NoteActionCodeID",Note.NoteActionCodeID+""},
				{"StatusID",Note.StatusID+""},{"Note",Note.Note+""},{"DateDue",Note.DateDue.Value.ToString("MM/dd/yyyy HH:mm")},
				{"DateDueBy",Note.DateDueBy+""}
				});
			} catch (Exception exc) { ASPException.Publish(exc); throw exc; }
			return Note_NewNoteInfo(Note.LoanID);
		}
		[WebMethod]
		public t_Note Note_NewNoteInfo(int LoanID) {
			try {
				var mb = new MotoBank();
				var dict = new t_Note();
				dict.DateDue = mb.NoteDateDue(0).Value;//.ToString("MM/dd/yy hh:mm tt");
				dict.DateDueBy = mb.Collection_CollectBy(LoanID);
				dict.LoanID = LoanID;
				return dict;
			} catch (Exception exc) { ASPException.Publish(exc); throw exc; }
		}
		[WebMethod]
		public List<v_Note_Type_ActionCode> Note_ActionCodesByNoteType(int NoteTypeID) {
			try {
				return new MotoBank().v_Note_Type_ActionCodes.Where(s => s.TypeID == NoteTypeID).OrderBy(s => s.IsTask).ThenBy(s => s.ActionLabel).ToList();
			} catch (Exception exc) { throw new ASPException(exc); }
		}
		[WebMethod]
		public List<v_NoteActionCode_Statuse> Note_StatusesByActionCode(int ActionCodeID) {
			try {
				return new MotoBank().v_NoteActionCode_Statuses.Where(s => s.NoteActionCodeID == ActionCodeID).OrderBy(s => s.Status).ToList();
			} catch (Exception exc) { ASPException.Publish(exc); throw exc; }
		}
		#endregion

		#region Collection Info
		[WebMethod]
		public string CollectionInfo(int LoanID, string UsixID, int CompanyID) {
			try {
				return "~/Controls/USIX/Bridge_CollectionInfo.ascx".LoadControl(new { LoanID = LoanID, UsixID = UsixID, CompanyID });
			} catch (Exception exc) {
				ASPException.Publish(exc);
				throw new Exception(ASPException.GetMessages(exc));
			}

		}
		#endregion

		#region AltiGen
		[WebMethod]
		public void OnHook(string Extention, string Password) {
			try {
				ProfileCommon p = Profiler.GetProfile();
				if (Extention != "") p.IPPhone.Extention = Extention; else Extention = p.IPPhone.Extention;
				if (Password != "") p.IPPhone.Password = Password; else Password = p.IPPhone.Password;
				p.Save();
				//if (!Regex.IsMatch(Extention, "^\\d{4}$")) throw new Exception("Extention must be four digits.");
				new Dialer(Extention, Password).OnHook();
			} catch (Exception exc) { throw new ClientException(exc); }
		}
		[WebMethod]
		public string Dial(string Extention, string Password, string PhoneNumber) {
			try {
				ProfileCommon p = Profiler.GetProfile();
				if (Extention != "") p.IPPhone.Extention = Extention; else Extention = p.IPPhone.Extention;
				if (Password != "") p.IPPhone.Password = Password; else Password = p.IPPhone.Password;
				p.Save();
				Data.SQL.ExecuteNonQuery("UPDATE t_ADS_Login SET Extension = @Ext WHERE sAMAccountName = @SAM AND ISNULL(Extension,'')<>@Ext", Extention, Lib.GetUserName(this.Context.User.Identity.Name));
				//if (!Regex.IsMatch(Extention, "^\\d{4}$")) throw new Exception("Extention must be four digits.");
				//if (!Regex.IsMatch(Password, "^\\d{4,5}$")) throw new Exception("Password must be four or five digits.");
				PhoneNumber = Regex.Replace(PhoneNumber, "[^0-9]", "").Trim();
				if (!Regex.IsMatch(PhoneNumber, "^(\\d{4}|\\d{10,})$")) throw new Exception("Phone must have four or 10+ digits.");
				try {
					new Dialer(Extention, Password, PhoneNumber).Dial();
				} catch (Dialer.OnHookException exc) {
					return exc.Message;
				}
			} catch (Exception exc) { throw new ClientException(exc); }
			return "";
		}
		#endregion

	}

}